﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Threading;

namespace PersonaLibrary.ViewModel
{
	public abstract class BaseViewModel : DispatcherObject, INotifyPropertyChanged, IViewModel
	{
		private DispatcherTimer _timer = null;

		public App CurrentApplication
		{
			get
			{
				return System.Windows.Application.Current as App;
			}
		}

		protected void RisePropertyChanged(string propertyName)
		{
			if (PropertyChanged != null) 
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

			if(_timer == null)
				_timer = new DispatcherTimer(TimeSpan.FromSeconds(1), DispatcherPriority.Normal, DispatcherTimerTick, Dispatcher);

			_timer.Stop();
			_timer.Start();
		}
		
		public event PropertyChangedEventHandler PropertyChanged;

		private void DispatcherTimerTick(object sender, EventArgs e)
		{
			DelayPropertyChanged();
		}

		public virtual string Name
		{
			get { return ToString(); }
		}

		protected virtual void DelayPropertyChanged()
		{
			
		}

		public virtual void Dispose()
		{
			GC.SuppressFinalize(this);
		}
	}
}
